package com.pqc.tbk.web;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pqc.tbk.service.account.ShiroDbRealm.ShiroUser;
import com.pqc.tbk.service.taobao.TaoBaoService;
import com.pqc.tbk.utils.UserUtil;
import com.taobao.api.domain.TaobaokeItem;

@Controller
@RequestMapping("/taobao")
public class TaobaoController {

	private TaoBaoService taoBaoService;

	@RequestMapping(value = "item", method = RequestMethod.POST)
	@ResponseBody
	public String item(@RequestParam("url") final String url) {
		final String id = getIdFromUrl(url);
		final ShiroUser user = UserUtil.getShiroUser();
		String outerCode = "1";
		if (user != null) {
			outerCode = user.getId() + "";
		}
		final List<TaobaokeItem> list = taoBaoService.getTBKItem(id, outerCode);
		final String json = toJson(list);
		return json;
	}

	private String toJson(final List<TaobaokeItem> list) {
		final ObjectMapper mapper = new ObjectMapper();
		mapper.setSerializationInclusion(Inclusion.NON_NULL);
		mapper.configure(
				DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		mapper.configure(
				DeserializationConfig.Feature.FAIL_ON_NUMBERS_FOR_ENUMS, true);
		String json = null;
		try {
			if ((list == null) || (list.size() == 0)) {
				json = "{\"numIid\":-1}";// 标记成没有商品返利的情况
			} else {
				final TaobaokeItem item = list.get(0);
				if (UserUtil.getShiroUser() == null) {
					item.setCommission("0");// 用户没有登录， 不让它显示佣金
				} else {
					item.setCommission(new DecimalFormat("#.00").format(Double
							.parseDouble(item.getCommission()) * 0.7));// 0.7是暂定的用户佣金分成比例
				}
				json = mapper.writeValueAsString(item);
			}
		} catch (final JsonGenerationException e) {
			json = "{\"numIid\":-1}";// 标记成没有商品返利的情况
			e.printStackTrace();
		} catch (final JsonMappingException e) {
			json = "{\"numIid\":-1}";// 标记成没有商品返利的情况
			e.printStackTrace();
		} catch (final IOException e) {
			json = "{\"numIid\":-1}";// 标记成没有商品返利的情况
			e.printStackTrace();
		}
		return json;
	}

	private String getIdFromUrl(final String url) {
		String id = "";
		final Matcher m = Pattern.compile("[\\?&]id=[\\d]+").matcher(url);
		if (m.find()) {
			id = m.group();
		}
		id = id.replaceAll("[^0-9]", "");
		return id;
	}

	@Autowired
	public void setTaobaoService(final TaoBaoService taoBaoService) {
		this.taoBaoService = taoBaoService;
	}
}
